Shared Access Signatures (1 / 7): You are developing a .NET application that interacts with Azure Blob Storage. You need to generate a token that will allow a client to have read and write access to both Blob and Queue services in your storage account for a period of 72 hours. This token should be generated using your storage account key and should be applicable at the storage account level. Write the C# code to accomplish this task.
// Define your storage account name and key
string accountName = "<storage-account-name>";
string accountKey = "<storage-account-key>";
Answer:
// Define your storage account name and key
string accountName = "<storage-account-name>";
string accountKey = "<storage-account-key>";
var sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
var sasBuilder = new AccountSasBuilder()
{
Services = AccountSasServices.Blobs | AccountSasServices.Queues,
ResourceTypes = AccountSasResourceTypes.All,
ExpiresOn = DateTimeOffset.UtcNow.AddDays(3),
Protocol = SasProtocol.Https
};
sasBuilder.SetPermissions(AccountSasPermissions.Read | AccountSasPermissions.Write);
// Use the key to get the SAS token
string sasToken = sasBuilder.ToSasQueryParameters(sharedKeyCredential).ToString();